package leetcodev1.数组;

public class LeetCode64 {

    //动态规划
    public int minPathSum(int[][] grid) {
        int rows = grid.length, columns = grid[0].length;
        int[][] dp = new int[rows][columns];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (i == 0 && j == 0) {
                    dp[0][0] = grid[0][0];//当前行
                    continue;
                }

                if (i == 0) {
                    dp[i][j] = dp[i][j - 1] + grid[i][j];//没得选
                    continue;
                }

                if (j == 0) {
                    dp[i][j] = dp[i - 1][j] + grid[i][j];//没得选
                    continue;
                }

                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];//选最小值
            }
        }
        return dp[rows - 1][columns - 1];
    }
}
